$(function(){
  let from =layui.from;
  let laypage=layui.laypage;
  let pagenum=1;
  let pagesize=3;
  function loadUserList(param){
    console.log(param);
    $.ajax({
      type:'get',
      url:'admin/users',
      data:param,
      success:function(res){
        let tags=template('table-tpl',res);
        $('.layui-table tbody').html(tags);
        laypage.render({
          elem:'articlePage',
          curr:pagenum,
          count:res.total,
          limit:pagesize,
          limit:[3,10,30,40,100],
          layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
          jump:function(obj,first){
            pagenum=obj.curr;
            pagesize=obj.limit;
            if(!first){
              loadUserList({
                pagenum:pagenum,
                pagesize:pagesize,

              })
            }
          }
        });

      }
    })
  }
  loadUserList({
    pagenum:pagenum,
    pagesize:pagesize,
  
  })
  $('.layui-table tbody').on('click', '.layui-btn-danger', function (e) {
    var id = $(e.target).data('id')
    layer.confirm('确认要删除用户吗？', function (index) {
      $.ajax({
        type: 'delete',
        url: 'admin/users/' + id,
        success: function (res) {
          layer.msg(res.message)
          loadUserList({
            pagenum: pagenum,
            pagesize: pagesize
          })
        }
      })
    })
  })
  $('.layui-table tbody').on('click', '.layui-btn-normal', function (e) {
    var id = $(e.target).data('id')
    var index = layer.open({
      type: 1,
      title: '重置密码',
      content: $('#repwd-form-tpl').html(),
      area: ['500px', '250px']
    })
    $('#repwd-form').submit(function (e) {
      e.preventDefault()
      $.ajax({
        type: 'put',
        url: 'admin/users/' + id,
        data: {
          password: $('#repwd-form input[name=password]').val()
        },
        success: function (res) {
          layer.msg(res.message)
          layer.close(index)
        }
      })
    })
  })
})
